package com.yogee.topseller.activity;

import android.text.TextUtils;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.yogee.core.base.HttpActivity;
import com.yogee.topseller.R;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.utils.LogUtils;

import butterknife.BindView;

/**
 * @auathor ${XiaoJianghong}
 * @date 2017/7/23.
 */

public class CompareProductActivity1 extends HttpActivity {
    @BindView(R.id.wb)
    WebView wb;
    @BindView(R.id.myProgressBar)
    ProgressBar bar;
    private String url;
    private String id;
    private String type;
    private String proId;
    private int num = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_compare_product;
    }

    @Override
    protected void initView() {
        id = AppUtil.getUserId(this);
        type = getIntent().getStringExtra("type");
        num = getIntent().getIntExtra("num",0);
//        if(num<3){
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//        }else
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        proId = getIntent().getStringExtra("proId");
        wb.getSettings().setDomStorageEnabled(true);
        wb.getSettings().setDatabaseEnabled(true);
        wb.getSettings().setJavaScriptEnabled(true);
        wb.getSettings().setDefaultTextEncodingName("GBK");
        wb.getSettings().setAllowFileAccessFromFileURLs(false);
        wb.getSettings().setAllowUniversalAccessFromFileURLs(false);

        wb.setWebViewClient(new WebViewClient());

        wb.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    bar.setVisibility(View.GONE);
                } else {
                    if (View.GONE == bar.getVisibility()) {
                        bar.setVisibility(View.VISIBLE);
                    }
                    bar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }
        });

        wb.addJavascriptInterface(this, "nativeMethod");
        if (type.equals("0")) {
            wb.loadUrl("file:///android_asset/jinxiaobang/searchA.html?userId=" + id);
        } else {
            if(TextUtils.isEmpty(proId)){
                wb.loadUrl("file:///android_asset/jinxiaobang/index.html?userId=" + id);
            }else{
                LogUtils.e("sfz","?userId=" + id +"&proId="+proId);
                wb.loadUrl("file:///android_asset/jinxiaobang/index1.html?userId=" + id +"&proId="+proId);
            }

        }
    }

    @JavascriptInterface
    public void toActivity(String activityName) {
        //此处应该定义常量对应，同时提供给web页面编写者
//        if (TextUtils.equals(activityName, "a")) {
//            CompareProductActivity.this.finish();
//        } else {
//            CompareProductActivity.this.finish();
//        }
        CompareProductActivity1.this.finish();
    }

}